package com.sto.stoapp.db.manager;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.sto.stoapp.db.StoDataBase;
import com.sto.stoapp.db.constant.RegionInfoConstant;
import com.sto.stoapp.db.constant.StoBaseConstant;
import com.sto.stoapp.http.response.download.info.RegionInfo;
import com.sto.stoapp.log.ExceptionLog;
import com.sto.stoapp.util.UnifiedStaticType;

/**
 * 行政区域管理类
 * 
 * @author cjb
 * 
 */
public class RegionInfoManager extends StoDataBase implements
		DownLoadDao<RegionInfo> {
	static RegionInfoManager mgr;

	private RegionInfoManager(Context context) {
		super(context);

	}

	public static RegionInfoManager getInstance(Context c) {
		if (mgr == null)
			mgr = new RegionInfoManager(c);
		return mgr;
	}

	public boolean insert(RegionInfo entity) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long res = -1;
			try {
				ContentValues values = new ContentValues();
				values.put(RegionInfoConstant.CODE, entity.code);
				values.put(RegionInfoConstant.LEVEL, entity.level);
				values.put(RegionInfoConstant.TYPE, entity.type);
				values.put(RegionInfoConstant.NAME, entity.name);
				values.put(RegionInfoConstant.PARENTCODE, entity.parentCode);
				values.put(RegionInfoConstant.PARENTNAME, entity.parentName);
				values.put(RegionInfoConstant.VERSION, entity.version);
				values.put(RegionInfoConstant.ID, entity.id);
				values.put(STATUS, entity.status);
				res = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE)
						.insert(RegionInfoConstant.REGION_INFO_TABLE, null,
								values);
				if (res > 0) {
					Log.d("插入一条行政区域信息", "成功");
				} else {
					Log.d("插入一条行政区域信息", "失败");
				}
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("insert", e.getMessage());
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
			return res > 0 ? true : false;
		}
	}

	@Override
	public String getMaxVersion() {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			String version = "0";
			Cursor c = null;
			try {
				SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.READ_DATABASE);
				c = db.rawQuery(getMaxVersionSql(REGION_INFO_TABLE), null);
				if (c != null && c.moveToNext()) {
					version = c.getString(c
							.getColumnIndex(StoBaseConstant.VERSION));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				closeCursor(c);
				closeDb(UnifiedStaticType.READ_DATABASE);
			}
			return version;
		}
	}

	@Override
	public boolean replace(ContentValues values) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long result = 0;
			try {
				SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
				result = db.replace(REGION_INFO_TABLE, null, values);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return result > 0;
		}
	}

	@Override
	public void deleteDataByStatus(String status) {
		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {

			try {
				SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
				db.delete(REGION_INFO_TABLE, STATUS + "=?",
						new String[] { status });
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}

		}
	}

	@Override
	public ContentValues buildContentValues(RegionInfo entity) {
		ContentValues values = new ContentValues();
		values.put(RegionInfoConstant.CODE, entity.code);
		values.put(RegionInfoConstant.LEVEL, entity.level);
		values.put(RegionInfoConstant.TYPE, entity.type);
		values.put(RegionInfoConstant.NAME, entity.name);
		values.put(RegionInfoConstant.PARENTCODE, entity.parentCode);
		values.put(RegionInfoConstant.PARENTNAME, entity.parentName);
		values.put(RegionInfoConstant.VERSION, entity.version);
		values.put(RegionInfoConstant.ID, entity.id);
		values.put(STATUS, entity.status);
		return values;
	}

	/**
	 * 插入多数据
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public void insertOrReplace(List<RegionInfo> datas) {

		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			SQLiteDatabase db = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE);
			try {
				db.beginTransaction();
				for (RegionInfo info : datas) {
					long res = -1;
					ContentValues values = buildContentValues(info);
					// res = db.insert(REGION_INFO_TABLE, null, values);
					// if (res > 0) {
					// Log.d("插入一条行政区域信息", "成功");
					// } else {
					if (replace(values)) {
						Log.d("插入一条行政区域信息", "成功");
					} else {
						Log.d("插入一条行政区域信息", "失败");
					}
					// }
				}
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("insertList", e.getMessage());
			} finally {
				datas.clear();
				db.setTransactionSuccessful();
				db.endTransaction();
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}
		}
	}

	@Override
	public void deleteAll() {

		synchronized (UnifiedStaticType.DATABASE_SYNCHRONIZED) {
			long res = -1;
			try {
				res = getSqliteDatabase(UnifiedStaticType.WRITE_DATABASE)
						.delete(REGION_INFO_TABLE, null, null);
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLog.writeLog("deleteAll", e.getMessage());
			} finally {
				closeDb(UnifiedStaticType.WRITE_DATABASE);
			}

		}

	}
}
